﻿using XDQ.VAT.EntityFrameworkCore;
using Volo.Abp.Autofac;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.Modularity;
using System.Reflection;
using Volo.Abp.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Volo.Abp.ObjectMapping;

namespace XDQ.VAT.DbMigrator
{
    [DependsOn(
        typeof(AbpAutofacModule),
        typeof(VATEntityFrameworkCoreDbMigrationsModule),
        typeof(VATApplicationContractsModule)
        )]
    public class VATDbMigratorModule : AbpModule
    {

        public override void PreConfigureServices(ServiceConfigurationContext context)
        {
            base.PreConfigureServices(context);
        }

        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpBackgroundJobOptions>(options => options.IsJobExecutionEnabled = false);

            Configure<Attributes.DefaultIdentityAttribute>(options =>
            {
                var configuration = context.Services.GetConfiguration();
                var data = configuration.GetSection("Identity");
                if (data.Exists())
                {
                    data.Bind(options);
                }
                else
                {
                    var att = Assembly.GetExecutingAssembly().GetCustomAttribute<Attributes.DefaultIdentityAttribute>();
                    options.From(att);
                }
            });

            Configure<Microsoft.AspNetCore.Identity.IdentityOptions>(options =>
            {
                var pwd = options.Password;
                pwd.RequireDigit = false;
                pwd.RequireLowercase = false;
                pwd.RequiredLength = 1;
                pwd.RequiredUniqueChars = 0;
                pwd.RequireUppercase = false;
                pwd.RequireNonAlphanumeric = false;
            });
        }
    }
}
